﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using umbraco;
using umbraco.BasePages;
using umbraco.BusinessLogic;
using umbraco.interfaces;
using umbraco.controls;
using umbraco.uicontrols;
using Commerce.Common;

namespace CommerceForUmbraco.adminSection.commerce
{
    public partial class editOrder : umbraco.BasePages.UmbracoEnsuredPage
    {
        #region properties

        public TabPage MainTab;
        public TabPage ItemTab;
        public TabPage NoteTab;
        public TabPage TransTab;

        private umbraco.controls.datePicker OrderDate;

        // order
        private Order thisOrder;

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            string orderId = Request.QueryString.Get("o");
            thisOrder = OrderController.GetOrder(int.Parse(orderId));

            if (!IsPostBack)
            {
                orderNumber.Text = thisOrder.OrderNumber;
                OrderDate.Text = thisOrder.OrderDate.ToLongDateString();
                orderStatusDisplay.Text = thisOrder.OrderStatus.ToString();

                billTo.Text = thisOrder.BillToAddress;
                shipTo.Text = thisOrder.ShipToAddress;
                emailAddress.Text = thisOrder.Email;
                CouponCodes.Text = thisOrder.CouponCodes;

                ShippingTypesCollection shipTypes = new ShippingTypesCollection().Load();
                shippingMethod.Items.Clear();
                foreach (ShippingTypes thisType in shipTypes)
                {
                    shippingMethod.Items.Add(new ListItem(thisType.ShippingType, thisType.ShippingTypeID.ToString()));
                }
                shippingMethod.SelectedIndex = shippingMethod.Items.IndexOf(shippingMethod.Items.FindByText(thisOrder.ShippingMethod));

                TrackingNumber.Text = thisOrder.ShippingTrackingNumber;

                // Items tab
                OrderItems.Text = thisOrder.ItemsToString(true);

                // Notes tab
                OrderStatussCollection orderStatuses = new OrderStatussCollection().Load();
                orderStatusDDL.Items.Clear();
                foreach (OrderStatuss thisStatus in orderStatuses)
                {
                    orderStatusDDL.Items.Add(new ListItem(thisStatus.OrderStatussX, thisStatus.OrderStatussID.ToString()));
                }
                orderStatusDDL.SelectedIndex = orderStatusDDL.Items.IndexOf(orderStatusDDL.Items.FindByValue(thisOrder.OrderStatusID.ToString()));

                gNotes.DataSource = thisOrder.Notes;
                gNotes.DataBind();

                // Transactions tab
                gTransactions.DataSource = thisOrder.Transactions;
                gTransactions.DataBind();

            }

                // Main tab
                this.Pane1.Text = "Order Details";
                this.pp_ordernumber.Text = "Order Number";
                this.pp_orderdate.Text = "Order Date";
                this.pp_orderStatus.Text = "Order Status";
                this.pp_billto.Text = "Bill To";
                this.pp_shipto.Text = "Ship To";
                this.pp_email.Text = "Email";
                this.pp_coupon.Text = "Coupon Used";
                this.pp_shipping_method.Text = "Shipping Method";
                this.pp_tracking.Text = "Tracking Number";
                this.pp_cancel.Text = "Cancel Order?";

                // Items tab
                this.Pane2.Text = "Order Items";
                this.pp_order_items.Text = "Order Items";

                // Notes tab
                this.Pane3.Text = "Order Notes";
                this.pp_notes.Text = "Order Note";
                this.pp_notestatus.Text = "Order Status";
                this.pp_notelist.Text = "Note History";

                // Transactions tab
                this.Pane4.Text = "Transactions";
                this.pp_transactions.Text = "Order Transactions";
            
        }

        protected override void OnInit(EventArgs e)
        {

            // Tab setup
            MainTab = tabControl.NewTabPage("Main");
            MainTab.Controls.Add(Pane1);

            ItemTab = tabControl.NewTabPage("Items");
            ItemTab.Controls.Add(Pane2);

            NoteTab = tabControl.NewTabPage("Notes");
            NoteTab.Controls.Add(Pane3);

            TransTab = tabControl.NewTabPage("Transactions");
            TransTab.Controls.Add(Pane4);


            //Add date control
            OrderDate = new datePicker();
            OrderDate.ID = "orderdate";
            OrderDate.ShowTime = true;

            pnlOrderDate.Controls.Add(OrderDate);

            // Main Tab
            ImageButton save = MainTab.Menu.NewImageButton();
            save.Click += new ImageClickEventHandler(this.save_click);
            save.AlternateText = "Save";
            save.ImageUrl = GlobalSettings.Path + "/images/editor/save.gif";

            // Item Tab
            ImageButton save2 = ItemTab.Menu.NewImageButton();
            save2.Click += new ImageClickEventHandler(this.save_click);
            save2.AlternateText = "Save";
            save2.ImageUrl = GlobalSettings.Path + "/images/editor/save.gif";

            // Note Tab
            ImageButton save3 = NoteTab.Menu.NewImageButton();
            save3.Click += new ImageClickEventHandler(this.save_click);
            save3.AlternateText = "Save";
            save3.ImageUrl = GlobalSettings.Path + "/images/editor/save.gif";


            base.Load += new EventHandler(this.Page_Load);
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
        }

        private bool SaveOrder(string orderId)
        {
            Order thisOrder = new Order(int.Parse(orderId));

            try
            {
                thisOrder.OrderDate = System.DateTime.Parse(OrderDate.Text);
                thisOrder.ShippingMethod = shippingMethod.SelectedItem.Text;

                thisOrder.Save("Commerce For Umbraco");

                return true;

            }
            catch 
            {
                return false;                
            }
        }
        
        #region button handlers

        private void save_click(object sender, ImageClickEventArgs e)
        {
            if (this.SaveOrder(base.Request.QueryString["o"]))
            {
                base.speechBubble(0, ui.Text("speechBubbles", "commerceItemSaved"), "");
            }
            else
            {
                base.speechBubble(0, ui.Text("speechBubbles", "commerceItemErrorHeader"), ui.Text("speechBubbles", "commerceItemErrorText"));
            }
        }

        protected void shipButton_Click(object sender, EventArgs e)
        {
            thisOrder.OrderStatus = OrderStatus.ShippedToCustomer;
            thisOrder.ShippingTrackingNumber = TrackingNumber.Text.Trim();
            thisOrder.Save("Commerce For Umbraco");
        }

        protected void cancelButton_Click(object sender, EventArgs e)
        {
            thisOrder.OrderStatus = OrderStatus.OrderCancelledPriorToShipping;
            thisOrder.Save("Commerce For Umbraco");
            
            // add a note
            OrderNote note = new OrderNote();
            note.OrderID = thisOrder.OrderID;
            note.OrderStatus = OrderStatus.OrderCancelledPriorToShipping.ToString();
            note.Note = cancelNote.Text;
            note.Save("Commerce For Umbraco");
            
            // refresh thisOrder object
            thisOrder = OrderController.GetOrder(thisOrder.OrderID);

            orderStatusDisplay.Text = thisOrder.OrderStatus.ToString();

            gNotes.DataSource = thisOrder.Notes;
            gNotes.DataBind();
        }

        protected void AddNoteButton_Click(object sender, EventArgs e)
        {
            OrderNote note = new OrderNote();
            note.OrderID = thisOrder.OrderID;
            note.OrderStatus = orderStatusDDL.SelectedItem.Text;
            note.Note = OrderNote.Text;
            note.Save("Commerce For Umbraco");

            // refresh thisOrder object
            thisOrder = OrderController.GetOrder(thisOrder.OrderID);

            gNotes.DataSource = thisOrder.Notes;
            gNotes.DataBind();

            OrderNote.Text = string.Empty;
        }
        
        #endregion


    }
}
